package com.neusoft.elm.controller;

import com.neusoft.elm.po.Cart;
import com.neusoft.elm.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/CartController")
public class CartController {
    @Autowired
    private CartService cartService;
    @PostMapping("/listCart")
    public Object listCart(Integer businessId,String userId) {
        try {
            Cart cart=new Cart();
            cart.setBusinessId(businessId);
            cart.setUserId(userId);
            List<Cart> cartList=cartService.listCart(cart);
            return cartList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/saveCart")
    public Object saveCart(Integer businessId,String userId,Integer foodId) {
        try {
            Cart cart=new Cart();
            cart.setBusinessId(businessId);
            cart.setUserId(userId);
            cart.setFoodId(foodId);
            cart.setQuantity(1);

            int i = cartService.saveCart(cart);
            return i;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }


    }
    @PostMapping("/removeCart")
    public Object removeCart(Integer businessId,String userId,Integer foodId) {
        try {
            Cart cart=new Cart();
            cart.setBusinessId(businessId);
            cart.setUserId(userId);
            cart.setFoodId(foodId);
            int i = cartService.removeCart(cart);
            return i;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }
    @PostMapping("/updateCart")
    public Object updateCart(Integer businessId,String userId,Integer foodId,Integer quantity) {
        try {
            Cart cart=new Cart();
            cart.setBusinessId(businessId);
            cart.setUserId(userId);
            cart.setFoodId(foodId);
            cart.setQuantity(quantity);
            int i = cartService.updateCart(cart);
            return i;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
